package com.ultimatech.feign.controller;

import com.netflix.discovery.EurekaClient;
import com.netflix.discovery.shared.Application;
import com.netflix.discovery.shared.Applications;
import com.ultimatech.cloud.poc.base.model.Message;
import com.ultimatech.cloud.poc.base.model.TestData;
import com.ultimatech.cloud.poc.base.model.UserVo;
import com.ultimatech.feign.service.ISchedualServiceHi;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

/**
 * Created by zhangleping on 2017/9/27.
 */
@RestController
public class HiController {

    @Autowired
    private ISchedualServiceHi serviceHi;

    @RequestMapping(value = "/hi",method = RequestMethod.GET)
    public String sayHi(@RequestParam String name){
        return serviceHi.sayHiFromClientOne(name);
    }

    @RequestMapping(value = "/whoami",method = RequestMethod.GET)
    public String whoami(@RequestParam String name){
        return serviceHi.whoami(name);
    }

    @RequestMapping(value = "/test",method = RequestMethod.GET)
    @ResponseBody
    public TestData getTestData(String name){
        return serviceHi.test(name);
    }

    @Autowired
    private EurekaClient eurekaClient;

    @RequestMapping(value = "/regInfo",method = RequestMethod.GET)
    public List<Application> getSvrs(){
        Set regions=eurekaClient.getAllKnownRegions();
        Applications applications=eurekaClient.getApplications();
        List<Application> apps=applications.getRegisteredApplications();
//        for(Application app:apps){
//            app.getInstances()
//        }
        return apps;
    }

    @RequestMapping(path = "/createUser",method = RequestMethod.POST)
    public Message<UserVo> createUser(UserVo user){
        return serviceHi.createUser(user);
    }

    @RequestMapping(path = "/createUser",method = RequestMethod.GET)
    public Message<UserVo> createUser(String name, String number){
        UserVo user=new UserVo();
        user.setName(name);
        user.setNumber(number);
        return serviceHi.createUser(user);
    }

    @RequestMapping(path = "/delUser")
    public Message<Integer> delUser(Integer userId){
        return this.serviceHi.delUser(userId);
    }

    @RequestMapping(path = "/updateUser",method = RequestMethod.POST)
    public Message<UserVo> updateUser(UserVo user){
        return serviceHi.updateUser(user);
    }

    @RequestMapping(path = "/updateUser",method = RequestMethod.GET)
    public Message<UserVo> updateUser(String id,String name,String number){
        UserVo user=new UserVo();
        user.setId(Integer.valueOf(id));
        user.setNumber(number);
        user.setName(name);
        return serviceHi.updateUser(user);
    }

    @RequestMapping(path = "/getuser")
    public Message<UserVo> getUserById(Integer id){
        return serviceHi.getUserById(id);
    }

    @RequestMapping(path = "/findAllUser")
    public Message<List<UserVo>> findAllUser(){
        return serviceHi.findAllUser();
    }
}
